package com.ruitu.health_2.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.arad.base.base_list.BaseRefreshActivity;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.support.widget.progress.ProgressLayout;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.health_2.R;
import com.ruitu.health_2.mvp.model.ProductModel;
import com.ruitu.health_2.mvp.presenter.ProductPresenter;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.CommonUtils;
import com.ruitu.router_module.bean.PageModel2;
import com.ruitu.router_module.bean.product.Coupon;
import com.ruitu.router_module.util.ReqUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.RecyclerView;
import okhttp3.RequestBody;

// 优惠券列表
public class CouponListActivity extends BaseRefreshActivity<ProductPresenter.CouponListP, ProductModel.CouponListM> {

    private CouponListAdapter adapter;
    private List<Coupon> couponList = new ArrayList<>();

    private int currPage = 1;
    private int totalPage = 1;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_coupon_list_2;
    }

    @Override
    protected void onItemClick(View v, int position) {
        // 领取优惠券
        if (CommonUtils.isLoginToActivity(this)) {
            Map<String, String> map = new HashMap<>();
            map.put("couponId", couponList.get(position).getId());
            map.put("memberId", AppHolder.instance().getUser().getId());
            RequestBody body = ReqUtil.getBody(map, true);
            p.reqGetCoupon(body);
            hideProgress();
            showProgressWithText(true, "领取中..");
        } else {
            ToastUtils.showShortSafe("请先登录");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setHeadTitle("优惠券列表");

        reqDataList();
        showProgress();
    }

    private void reqDataList() {
//        p.reqVideoList(20, currPage);
        p.reqCouponList(20, currPage);
    }

    @Override
    public void onReqComplete(int code, boolean isOk, Object data) {
        if (code == 1) {// 优惠券列表
            if (currPage == 1) {
                couponList.clear();
            }
            PageModel2<Coupon> couponPage = (PageModel2<Coupon>) data;
            totalPage = couponPage.getTotalPage();
            List<Coupon> newData = couponPage.getList();
            couponList.addAll(newData);
            adapter.setDataList(couponList);
        }
        if (code == 2) {// 领券
            ToastUtils.showShortSafe("优惠券已领取");
        }
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new CouponListAdapter(this);
        return adapter;
    }

    @Override
    protected LRecyclerView findRecyclerView() {
        return findViewById(R.id.lrcv_list);
    }

    @Override
    protected void reqFirst() {
        currPage = 1;
        reqDataList();
    }

    @Override
    protected void reqMore() {
        currPage++;
        if (currPage <= totalPage) {
            reqDataList();
        } else {
            refreshNoMoreData();
        }
    }

    private static class CouponListAdapter extends ListBaseAdapter<Coupon> {

        public CouponListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_coupon_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_amount = holder.getView(R.id.tv_amount);
            TextView tv_time = holder.getView(R.id.tv_time);

            Coupon coupon = mDataList.get(position);
            tv_name.setText(coupon.getName());
            tv_amount.setText("￥" + coupon.getAmount());
            tv_time.setText(coupon.getEndTime() + "前有效");
        }
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }
}
